package com.cy.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cy.bean.Show;
import com.cy.service.MainService;
import com.cy.service.MainServiceImpl;

import net.sf.json.JSONArray;

/**
 * Servlet implementation class MainServlet
 */
@WebServlet("/main")
public class MainServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置前端页面传递回来的字符编码
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		response.setContentType("application/json; charset=utf-8");
		String action = request.getParameter("action");
		if (action == null || action == "") {
			request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response);
		} else if (action != null && action.equals("show")) {
			MainService mainService = new MainServiceImpl();
			List<Show> showvalue = mainService.show();
			JSONArray json = JSONArray.fromObject(showvalue);
			response.getWriter().print(json);
		}
	}

}
